var Options;

Options = function () {
    var $, settings, initForm, setSelectBoxValue, setRadioInputsValue,
            setValues, initThumbCyclePeriod, initNotificationTimeout,
            initDonationHider, switchDonation;
    
    settings = new Settings();
    
    $ = function (selector) {
        return document.querySelectorAll(selector);
    };
    
    setSelectBoxValue = function (select, value) {
        var options, i;
        options = select.options;
        for (i = options.length; i--;) {
            options[i].selected = options[i].value == value;
        }
    };
    
    setRadioInputsValue = function (name, value) {
        var inputs, i;
        inputs = $('input[name="' + name + '"]');
        for (i = inputs.length; i--;) {
            inputs[i].checked = inputs[i].value == value;
        }
    };
    
    setValues = function () {
        setSelectBoxValue($('#subsTimeout')[0], settings.getSubsTimeout());
        setSelectBoxValue($('#displayedVideos')[0],
                settings.getDisplayedVideos());
        setSelectBoxValue($('#pollingPeriod')[0], settings.getPollingPeriod());
        setSelectBoxValue($('#ignore')[0], settings.getIgnore());
        setRadioInputsValue('sorting', settings.getSorting());
        setRadioInputsValue('removeWatched', settings.getRemoveWatched());
        $('#thumbCyclePeriod')[0].value = settings.getThumbCyclePeriod();
        setRadioInputsValue('primarySort', settings.getPrimarySort());
        setRadioInputsValue('enableNotifications',
                settings.getEnableNotifications());
        $('#notificationTimeout')[0].value = settings.getNotificationTimeout();
        setSelectBoxValue($('#middleClickBehaviour')[0],
                settings.getMiddleClickBehaviour());
    };
    
    initForm = function () {
        var form, value, background;
        form = $('form')[0];
        form.addEventListener('submit', function (e) {
            e.preventDefault();
            settings.setSubsTimeout($('#subsTimeout')[0].value);
            settings.setDisplayedVideos($('#displayedVideos')[0].value);
            settings.setPollingPeriod($('#pollingPeriod')[0].value);
            settings.setSorting($('input[name="sorting"]')[1].checked ? 0 : 1);
            settings.setIgnore($('#ignore')[0].value);
            settings.setRemoveWatched($('input[name="removeWatched"]')[0].
                    checked ? '1' : '');
            value = parseFloat($('#thumbCyclePeriod')[0].value) * 10;
            settings.setThumbCyclePeriod(Math.round(value) / 10);
            settings.setPrimarySort($('input[name="primarySort"]')[0].checked ?
                    '' : '1');
            settings.setEnableNotifications(
                    $('input[name="enableNotifications"]')[0].checked ?
                    '1' : '');
            settings.setNotificationTimeout(
                    $('#notificationTimeout')[0].value);
            settings.setMiddleClickBehaviour(
                    $('select[name="middleClickBehaviour"]')[0].value);
            background = chrome.extension.getBackgroundPage();
            (new background.Background()).reload();
            $('div')[0].innerHTML = '<span>Saved</span>';
        }, false);
    };
    
    initThumbCyclePeriod = function () {
        var em, input, value;
        input = $('#thumbCyclePeriod')[0];
        em = input.parentNode.getElementsByTagName('em')[0];
        value = Math.round(parseFloat(input.value) * 10);
        em.innerText = value ? value / 10 + ' s' : 'off';
        input.addEventListener('change', function () {
            value = Math.round(parseFloat(input.value) * 10);
            em.innerText = value ? value / 10 + ' s' : 'off';
        }, false);
    };
    
    initNotificationTimeout = function () {
        var em, input;
        input = $('#notificationTimeout')[0];
        em = $('#notificationTimeout + em')[0];
        em.innerText = input.value == '0' ? 'never' : input.value + ' s';
        input.addEventListener('change', function () {
            em.innerText = input.value == '0' ? 'never' : input.value + ' s';
        }, false);
    };
    
    initDonationHider = function () {
        var switcher;
        switcher = document.getElementById('donate-switch').
                getElementsByTagName('a')[0];
        switcher.addEventListener('click', function (e) {
            e.preventDefault();
            switchDonation();
            switcher.innerHTML =
                    (settings.getHideDonation() ? 'show' : 'hide') +
                    ' donation options &times;';
        }, false);
        if (settings.getHideDonation()) {
            document.getElementById('donate').style.display = 'none';
            document.getElementById('donate-form').style.display = 'none';
            switcher.innerHTML = 'show donation options &times;';
        }
    };
    
    switchDonation = function () {
        var display;
        display = settings.getHideDonation() ? '' : 'none';
        document.getElementById('donate').style.display = display;
        document.getElementById('donate-form').style.display = display;
        settings.setHideDonation(display ? 'true' : '');
    };
    
    setValues();
    initForm();
    initThumbCyclePeriod();
    initNotificationTimeout();
    initDonationHider();
};

addEventListener('load', function () {
    new Options();
}, false);
